package com.xutong.myfunction.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.xutong.myfunction.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LuckyDrawActivity extends AppCompatActivity {

    private GridView gvLuckyDraw;
    private List<String> list;
    private Button btnStart;
    private LuckyDrawAdapter luckyDrawAdapter;

    private boolean isRun = false;
    private int sum = 0;

    private Random random;
    private String[] strList = new String[]{"快餐","拉面","火锅粥","蒸菜","烧烤","汤粉面","湘菜馆","木桶饭","蒸饭","沙县","牛杂锅","烧腊","麻辣烫","烤鱼","饺子馆","乐兹士"};

    private int total;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lucky_draw);

        gvLuckyDraw = (GridView) findViewById(R.id.gvLuckyDraw);
        btnStart = (Button) findViewById(R.id.btnStart);

        list = new ArrayList<>();
        for (int i=0;i<strList.length;i++){
            list.add(strList[i]);
        }

        luckyDrawAdapter = new LuckyDrawAdapter(this);
        gvLuckyDraw.setAdapter(luckyDrawAdapter);

        random = new Random();

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            isRun = true;
            new Thread(runnable).start();
            btnStart.setClickable(false);
            }
        });

        total = list.size();

        gvLuckyDraw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Toast.makeText(LuckyDrawActivity.this,"点击了第"+(i+1)+"个",Toast.LENGTH_SHORT).show();
                String name = (String) adapterView.getItemAtPosition(i);
                shouwDeleteDialog(name,i);
            }
        });


    }

    private void shouwDeleteDialog(String name, final int index){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("提示");
        builder.setMessage("是否删除"+name);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                list.remove(index);
                luckyDrawAdapter.notifyDataSetChanged();
                total--;
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.show();
    }

    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            int r = random.nextInt(total);
            luckyDrawAdapter.setSelectorIndex(r);
            luckyDrawAdapter.notifyDataSetChanged();
            sum++;
            if (sum>=total){
                sum=0;
                isRun=false;
                btnStart.setClickable(true);
                String lucky = (String) luckyDrawAdapter.getItem(luckyDrawAdapter.selectorIndex);

                showDialog("您抽中了"+lucky);
            }
            super.handleMessage(msg);
        }
    };

    Runnable runnable = new Runnable() {
        @Override
        public void run() {

             while (isRun){

                Message message = new Message();
                handler.sendMessage(message);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        }
    };

    private void showDialog(String msg){

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(LuckyDrawActivity.this);
        alertDialog.setTitle("恭喜");
        alertDialog.setMessage(msg);
        alertDialog.show();

    }

    class LuckyDrawAdapter extends BaseAdapter{
        private int selectorIndex=16;
        private Context context;
        private LayoutInflater inflater;
        public LuckyDrawAdapter(Context context){
            this.context = context;
            this.inflater = LayoutInflater.from(this.context);
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            try {
                return list.get(i);
            }catch (Exception e){
                return null;
            }

        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        public void setSelectorIndex(int index){
            selectorIndex = index;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            TextView textView=null;
            if (view==null){
                view = inflater.inflate(R.layout.item,null);
            }
            if (selectorIndex == i){
                view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            }else {
                view.setBackgroundColor(getResources().getColor(R.color.white));
            }
            textView = (TextView) view.findViewById(R.id.tv);
            textView.setText(list.get(i));
            return view;
        }
    }

}
